package handler;

import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONObject;

import pojo.bean.Course;
import pojo.dao.CourseDAO;
import pojo.dao.SpecialtyDAO;
import pojo.dao.StageDAO;
import pojo.mapper.CourseMapper;
import pojo.mapper.SpecialtyMapper;
import pojo.mapper.StageMapper;
import tool.JSONObjectConverter;
import tool.MapperGenerator;

public class ComboBoxRequestHandler extends HttpServlet {
    private static final long serialVersionUID = 1715901890136507374L;
    private SpecialtyMapper specialtyMapper;
    private StageMapper stageMapper;
    private CourseMapper courseMapper;

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("application/json;charset=UTF-8");
        String type = req.getParameter("type");
        if (Objects.isNull(type) || type.length() == 0) {
            return;
        }
        PrintWriter out = resp.getWriter();
        List<?> elements;
        List<JSONObject> objects;
        switch (type) {
        case "specialty": {
            elements = specialtyMapper.selectAll();
            objects = JSONObjectConverter.convert(elements);
            JSONObject[] array = objects.toArray(new JSONObject[objects.size()]);
            out.print(Arrays.toString(array));
            break;
        }
        case "stage": {
            elements = stageMapper.selectAll();
            objects = JSONObjectConverter.convert(elements);
            JSONObject[] array = objects.toArray(new JSONObject[objects.size()]);
            out.print(Arrays.toString(array));
            break;
        }
        case "course": {
            int specialtyId = Integer.parseInt(req.getParameter("specialtyId"));
            int stageId = Integer.parseInt(req.getParameter("stageId"));
            Course temporary = new Course(specialtyId, stageId);
            Course course = courseMapper.selectByForeignKeys(temporary);
            JSONObject[] array = new JSONObject[1];
            if (Objects.nonNull(course)) {
                array[0] = new JSONObject(course);
            } else {
                array[0] = null;
            }
            out.print(Arrays.toString(array));
            break;
        }
        }
    }

    @Override
    public void init() throws ServletException {
        try {
            specialtyMapper = MapperGenerator.generate(SpecialtyDAO.class);
            stageMapper = MapperGenerator.generate(StageDAO.class);
            courseMapper = MapperGenerator.generate(CourseDAO.class);
        } catch (NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException
            | IllegalArgumentException | InvocationTargetException e) {
            e.printStackTrace();
        }
        super.init();
    }
}
